package com.jinlejia.erp.utils;

import android.content.Context;

import com.zhy.http.okhttp.OkHttpUtils;

import java.io.File;
import java.io.IOException;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import rx.Observable;
import rx.Subscriber;

/**
 * Created by Administrator on 2017/3/7 0007.
 */

public class RxUpLoad {
    private static RxUpLoad INSTANCE;
    private static Context mContext;
    private String actionUrl;
    private File file;
    private String fileName;
    private String key;
    private Map<String, String> headParams;


    public static RxUpLoad get(Context context) {
        if (INSTANCE == null)
            INSTANCE = new RxUpLoad();
        mContext = context;
        return INSTANCE;
    }

    //上传的图片
    public RxUpLoad file(File f) {
        file = f;
        return this;
    }

    //上传地址
    public RxUpLoad url(String url) {
        actionUrl = url;
        return this;
    }

    //文件名
    public RxUpLoad fileName(String fName) {
        fileName = fName;
        return this;
    }

    //服务器接收文件的key
    public RxUpLoad key(String k) {
        key = k;
        return this;
    }

    //http 请求头
    public RxUpLoad header(Map<String, String> heads) {
        headParams = heads;
        return this;
    }

    public Observable<String> asObservable() {
        return Observable.create(new Observable.OnSubscribe<String>() {

            @Override
            public void call(final Subscriber<? super String> subscriber) {
                final MediaType MEDIA_TYPE_PNG = MediaType.parse("image/png");
                MultipartBody.Builder multipartBodyBuilder = new MultipartBody.Builder();
                multipartBodyBuilder.setType(MultipartBody.FORM);
                //遍历map中所有参数到builder
                if (headParams != null) {
                    for (String key : headParams.keySet()) {
                        multipartBodyBuilder.addFormDataPart(key, headParams.get(key));
                    }
                }
                //遍历paths中所有图片绝对路径到builder，并约定key如“upload”作为后台接受多张图片的key
                if (file != null) {
                    multipartBodyBuilder.addFormDataPart(key, fileName, RequestBody.create(MEDIA_TYPE_PNG, file));
                }
                //构建请求体
                RequestBody requestBody = multipartBodyBuilder.build();

                Request.Builder RequestBuilder = new Request.Builder();
                RequestBuilder.url(actionUrl);// 添加URL地址
                RequestBuilder.post(requestBody);
                Request request = RequestBuilder.build();
                OkHttpClient mOkHttpClient = OkHttpUtils.getInstance().getOkHttpClient();
                mOkHttpClient.newCall(request).enqueue(new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {
                        subscriber.onError(e);
                        subscriber.onCompleted();
                        call.cancel();
                    }

                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        String str = response.body().string();
                        subscriber.onNext(str);
                        subscriber.onCompleted();
                    }
                });
            }
        });
    }

}
